/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.data;
import java.sql.*;
import java.util.*;
/**
 *
 * @author Guesmi
 */
public class Attachement extends ITable {

    private int ATTCH_ID ;
    private String FILE_TYPE ;
    private String File_NOM ;
    private int REC_ID ;
    private String ATTCH_MSG ;

    private static final String Table ="ATTACHEMENT";
    
    public int GetIDAttachement(){return this.ATTCH_ID ;}
    public String GetTyupeFichier(){return this.FILE_TYPE ;}
    public String GetNomFichier(){return this.File_NOM ;}
    public int GetIDReclamation(){return this.REC_ID ;}
    public String GetMSGAttachement(){return this.ATTCH_MSG ;}

    public void SetIDAttachement(int id){this.ATTCH_ID = id ;}
    public void SetTyupeFichier(String fich){this.FILE_TYPE = fich ;}
    public void SetNomFichier(String fich){this.File_NOM = fich ;}
    public void SetIDReclamation(int rec){this.REC_ID = rec ;}
    public void SetMSGAttachement(String msg){this.ATTCH_MSG = msg ;}

    public void LoadFromDB (String MAT){
        //ConnexionBD db = new ConnexionBD();
        try{
        ResultSet res = db.executerSelect(" Select * from "+ Attachement.Table +" where ATTCH_ID ='" + MAT + "'");
        if(res.next())
             {
                this.ATTCH_ID = res.getInt("ATTCH_ID");
                this.FILE_TYPE = res.getString("FILE_TYPE");
                this.File_NOM = res.getString("File_NOM");
                this.REC_ID = res.getInt("REC_ID");
                this.ATTCH_MSG = res.getString("ATTCH_MSG");
             }
        }catch(Exception sql){}
    }

    public void SaveToDB() throws Exception
    {
            db.InsertNewLine(Attachement.Table ,
                    "ATTCH_ID",Integer.toString(this.ATTCH_ID),
                    "FILE_TYPE","'"+ this.FILE_TYPE +"'",
                    "File_NOM","'"+ this.File_NOM +"'",
                    "REC_ID",Integer.toString(this.REC_ID),
                    "ATTCH_MSG","'"+ this.ATTCH_MSG +"'");
    }


    public static Attachement SearchByID(int ID)
    {
        Attachement att = new Attachement();
        try{
        ResultSet res = db.executerSelect("select * from "+ Attachement.Table +" where ATTCH_ID ='"+ ID +"'");
            while (res.next())
            {
                att.ATTCH_ID = res.getInt("ATTCH_ID");
                att.FILE_TYPE = res.getString("FILE_TYPE");
                att.File_NOM = res.getString("File_NOM");
                att.REC_ID = res.getInt("REC_ID");
                att.ATTCH_MSG = res.getString("ATTCH_MSG");

            }
        }catch(Exception sqle){}
        return att ;
    }

    public static ArrayList<Attachement> getAll()
    {
        ArrayList<Attachement> list = new ArrayList<Attachement>();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table);
            if(res.next())
            {
                Attachement att = new Attachement();
                att.ATTCH_ID = res.getInt("ATTCH_ID");
                att.FILE_TYPE = res.getString("FILE_TYPE");
                att.File_NOM = res.getString("File_NOM");
                att.REC_ID = res.getInt("REC_ID");
                att.ATTCH_MSG = res.getString("ATTCH_MSG");
                list.add(att);
            }
            System.out.println("La liste contient :"+ list.size() +" Attaches .");
        }catch(Exception sqle){}
        return list ;
    }

}